package com.inphase.doc;

import com.inphase.doc.config.OfficeWeb365Properties;
import com.inphase.doc.config.WopiProperties;
import com.inphase.doc.service.WopiService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@SpringBootApplication
@Controller
@EnableConfigurationProperties({WopiProperties.class,OfficeWeb365Properties.class})
public class DemoApplication {



    @Autowired
    WopiService wopiService;



    @RequestMapping("/")
    public String index(){
        return "redirect:/index.html";
    }


    /**
     * 预览
     * @param response
     * @param fileId
     * @throws IOException
     */
    @RequestMapping("/preview/{fileId}")
    public void preview(HttpServletResponse response,@PathVariable  String fileId) throws IOException {
        String url = wopiService.clientUrl(fileId);
        response.sendRedirect(url);
    }

    /**
     * 加载实际文件
     */
    @RequestMapping("/wopi/files/{uuid}.{ext}")
    public void wopiService(@PathVariable String uuid,OutputStream outputStream) throws IOException {
        InputStream inputStream = (new ClassPathResource("myresource/1.doc")).getInputStream();
        IOUtils.copy(inputStream,outputStream);
    }















    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class);
    }
}
